function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}
function buildTrees(arr) {
    let root = null;
    if (arr.length > 0) {
        root = new TreeNode(arr[0]); // 根节点
        let queue = [root]; // 用队列辅助构建二叉树
        let i = 1;
        while (i < arr.length) {
            let current = queue.shift(); // 取出队首节点
            if (arr[i] !== null) {
                current.left = new TreeNode(arr[i]); // 构建左子节点
                queue.push(current.left); // 将左子节点加入队列
            }
            i++;
            if (i < arr.length && arr[i] !== null) {
                current.right = new TreeNode(arr[i]); // 构建右子节点
                queue.push(current.right); // 将右子节点加入队列
            }
            i++;
        }
    }
    return root
}

var traverse = function (queue, res) {
    let layerNodes = []
    while (queue.length !== 0) {
        layerNodes.push(queue.shift())
    }
    layerNodes.forEach((node) => {
        if (node.left)
            queue.push(node.left)
        if (node.right)
            queue.push(node.right)
    })
    res.push(layerNodes.map((node) => node.val))
    if (queue.length)
        traverse(queue, res)
}
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrder = function (root) {
    const queue = [];
    const res = []; 
    if(root != null && root.length > 0) {
        let treeRoot = buildTrees(root)
        // console.log(treeRoot)
        if (!treeRoot)
            return
        queue.push(treeRoot)
        traverse(queue, res)
    }
    console.log(res)
};
levelOrder([3,9,20,null,null,15,7])